// 2017-2-22
var xsIndex = {};
xsIndex.initPage = function () {
    // 2017-2-20
    $('.treeview-menu a').click(function () {
        var url = $(this).attr('href');
        xsIndex.xsfnLoadIframe(url);
        Cookies.set('url', url);
        return false;
    });
    xsIndex.xsfnLastVisit();
    xsIndex.xsfnButtonInit();
}
// 2017-2-20
xsIndex.xsfnSetIframeHeight = function (iframe) {
    var height;
    if (iframe.Document) {
        height = iframe.Document.body.scrollHeight;
    } else {
        height = iframe.contentDocument.body.scrollHeight;
    }
    iframe.style.height = height + 'px';
}

// 2017-2-21
xsIndex.xsfnLastVisit = function () {
    var url = Cookies.get('url');
    if (!url) {
        url = $('a.logo').attr('href');
    }
    xsIndex.xsfnLoadIframe(url);
}
/* 动态加载iframe 2016-3-10 SoChishun Added. */
xsIndex.xsfnLoadIframe = function (ps_url) {
    /* 销毁iframe，释放iframe所占用的内存(把iframe指向空白页面，这样可以释放大部分内存). */
    var iframe = $('.content-wrapper iframe')[0];
    if (iframe) {
        iframe.src = 'about:blank';
        try {
            iframe.contentWindow.document.write('');
            iframe.contentWindow.document.clear();
        } catch (e) {
        }
        iframe.parentNode.removeChild(iframe); // 把iframe从页面移除
    }
    var oIfrm = document.createElement("IFRAME"); // 创建iframe
    oIfrm.src = ps_url;
    oIfrm.scrolling = "no";
    oIfrm.frameBorder = 0;
    oIfrm.width = '100%';
    oIfrm.height = $('.content-wrapper').height() - 5;
    $('.content-wrapper').append(oIfrm);
    // 页面加载提示(loading) 2016-3-14
    var loading = '<div id="loading" style="font-size:12px; position: absolute; left:50%; top:50%; background-color:#FFF; border:solid 1px #CCC; padding:10px 20px; color:#888; z-index: 999">页面加载中, 请稍后...</div>';
    $('body').append(loading);
    $(oIfrm).load(function () {
        $('body').find('#loading').remove();
        xsIndex.xsfnSetIframeHeight(oIfrm);
    })
}

// 2017-3-17
xsIndex.xsfnButtonInit = function () {
    // 新增 2017-2-14
    $('.xsui-btn-add-dialog, .xsui-btn-edit-dialog').click(function () {
        xsIndex.xsfnDialogForm($(this));
        return false;
    })
}
/**
 * 
 * @param {type} $btn
 * @returns {Boolean}
 * @since 1.0 2017-3-16
 * @since 1.1 2017-3-18 新增gridChecked参数
 */
xsIndex.xsfnDialogForm = function ($btn) {
    var url = $btn.attr('href') ? $btn.attr('href') : $btn.data('url');
    if (!url) {
        alert('URL无效!');
        return false;
    }
    var title = $btn.attr('title') ? $btn.attr('title') : $btn.text();
    var options = {width: 768, height: 520};
    var soption = $btn.data('xsuiOption'); // 控件的[data-xsui-option]属性自定义选项：<a data-xsui-option="{'key':'value'}" />
    if (soption) {
        options = xsfn_extend_options(options, soption);
    }
    //iframe窗
    layer.open({
        type: 2,
        title: title,
        closeBtn: 1, //不显示关闭按钮
        shade: [0],
        //area: [options.width + 'px', options.height + 'px'],
        area: ['50vw', '50vh'],
        //offset: 'rb', //右下角弹出
        //time: 2000, //2秒后自动关闭
        maxmin: true, //开启最大化最小化按钮
        offset: [160, 0],
        anim: 2,
        content: [url, 'no'], //iframe的url，no代表不显示滚动条
    });
}